/**
 * Copyright 2005 SIB Framework

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.sibframework.sda.reflect;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.sibframework.sda.SdaFactory;
import org.sibframework.sda.SimpleDataAccess;
import org.sibframework.sda.dbmodel.Column;
import org.sibframework.sda.dbmodel.Table;


public class MysqlReflect implements IDBReflect {

	@Override
	public List<String> showSchemas() throws Exception {
		String sql = "show schemas";
		return SdaFactory.getInstance().queryStrings(sql);
	}

	@Override
	public List<Table> showTables() throws Exception {
		return this.showTables(null); //SimpleDataAccess.getInstance().queryStrings(sql);
	}
	
	@Override
	public List<Table> showTables(String schema) throws Exception {

		
		String sql = "show tables";
		if (schema != null && schema.length() > 0) {
			sql += " in " + schema;
		}
		return null;//SimpleDataAccess.getInstance().queryStrings(sql);
	}

	@Override
	public List<Column> showColumns(String tableName) throws Exception {
		
		Map<String, String> javaTypes = SdaFactory.getInstance().getColumnJavaType(tableName);
		//get fields from table
		List<Map<String, Object>> cos = SdaFactory.getInstance().query2Map("show columns from " + tableName);
		List<Column> columns = new ArrayList<Column>();
		//fields declare.
		for (Map<String, Object> map : cos) {
			Column col = new Column();
			col.setName((String) map.get("columnName"));
			col.setType((String) map.get("columnType"));
			col.setJavaType(TableType2JavaType.map(javaTypes.get(col.getName())));
			col.setPrimaryKey("PRI".equalsIgnoreCase((String) map.get("columnKey")));
			columns.add(col);
		}
		return columns;
	}

	@Override
	public List<Table> showTableColumns(String schema) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
